{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IMO 2024 Problem 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Related prompts:\n",
    "- Generate useful functions: https://shared-chats.lookeng.cn/IMO_2024_p3_gen_funcs.html\n",
    "- Integer Sequence Analysis: https://shared-chats.lookeng.cn/Integer_Sequence_Analysis.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from basic import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 1 Result:\n",
      "('even', 3, [2, 1, 3])\n"
     ]
    }
   ],
   "source": [
    "initial_terms = [1, 2, 3]\n",
    "num_terms = 100\n",
    "\n",
    "result = analyze_sequences(initial_terms, num_terms)\n",
    "print(\"Example 1 Result:\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2 -- 观察循环节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| Initial Terms   | Sequence Type   |   Cycle Start | Cycle Content                                   |\n",
      "|:----------------|:----------------|--------------:|:------------------------------------------------|\n",
      "| [1]             | even            |             0 | [1]                                             |\n",
      "| [2]             | odd             |             0 | [2, 1]                                          |\n",
      "| [3]             | even            |             3 | [2, 3, 1]                                       |\n",
      "| [4]             | odd             |             7 | [3, 4, 1, 2]                                    |\n",
      "| [5]             | even            |            11 | [4, 5, 1, 2, 3]                                 |\n",
      "| [6]             | odd             |            17 | [5, 6, 1, 2, 3, 4]                              |\n",
      "| [7]             | even            |            23 | [6, 7, 1, 2, 3, 4, 5]                           |\n",
      "| [8]             | odd             |            31 | [7, 8, 1, 2, 3, 4, 5, 6]                        |\n",
      "| [9]             | even            |            39 | [8, 9, 1, 2, 3, 4, 5, 6, 7]                     |\n",
      "| [10]            | odd             |            49 | [9, 10, 1, 2, 3, 4, 5, 6, 7, 8]                 |\n",
      "| [11]            | even            |            59 | [10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9]             |\n",
      "| [12]            | odd             |            71 | [11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]         |\n",
      "| [13]            | even            |            83 | [12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]     |\n",
      "| [14]            | odd             |            97 | [13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |\n",
      "\n",
      "| Initial Terms    | Sequence Type   |   Cycle Start | Cycle Content                   |\n",
      "|:-----------------|:----------------|--------------:|:--------------------------------|\n",
      "| [2, 9, 3, 2]     | even            |            43 | [8, 9, 3, 2, 1, 4, 5, 6, 7]     |\n",
      "| [9, 7, 9, 3, 7]  | even            |            50 | [9, 3, 2, 4, 5, 6, 7, 1, 8]     |\n",
      "| [3, 3]           | even            |             5 | [3, 1, 2]                       |\n",
      "| [10, 5, 9]       | odd             |            47 | [6, 7, 8, 9, 2, 3, 4, 5, 1, 10] |\n",
      "| [10, 1, 1, 7]    | odd             |            51 | [9, 10, 3, 4, 5, 6, 7, 1, 2, 8] |\n",
      "| [8, 8, 3, 5, 10] | odd             |            58 | [9, 10, 6, 7, 8, 2, 4, 5, 3, 1] |\n",
      "| [8, 8]           | odd             |            33 | [8, 1, 2, 3, 4, 5, 6, 7]        |\n",
      "| [6, 7, 1, 5]     | even            |            24 | [6, 2, 3, 4, 5, 1, 7]           |\n",
      "| [2, 5, 4]        | even            |            11 | [3, 4, 2, 1, 5]                 |\n",
      "| [3, 6, 8, 5]     | odd             |            40 | [7, 8, 6, 4, 5, 2, 3, 1]        |\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import random\n",
    "from basic import analyze_sequences, generate_sequence, split_sequence\n",
    "\n",
    "def generate_random_initial_terms(num_terms, max_value=10):\n",
    "    \"\"\"\n",
    "    生成一个随机的初始条件列表。\n",
    "\n",
    "    参数:\n",
    "    num_terms (int): 初始条件列表的长度\n",
    "    max_value (int): 初始条件中最大的整数值\n",
    "\n",
    "    返回:\n",
    "    List[int]: 随机生成的初始条件列表\n",
    "    \"\"\"\n",
    "    return [random.randint(1, max_value) for _ in range(num_terms)]\n",
    "\n",
    "def analyze_initial_terms(initial_terms_list, num_terms, min_repeat_len=None):\n",
    "    results = []\n",
    "\n",
    "    for initial_terms in initial_terms_list:\n",
    "        result = analyze_sequences(initial_terms, num_terms, min_repeat_len)\n",
    "        results.append({\n",
    "            \"Initial Terms\": initial_terms,\n",
    "            \"Sequence Type\": result[0],\n",
    "            \"Cycle Start\": result[1],\n",
    "            \"Cycle Content\": result[2]\n",
    "        })\n",
    "\n",
    "    df = pd.DataFrame(results)\n",
    "    print(df.to_markdown(index=False))\n",
    "\n",
    "# 特殊情形\n",
    "initial_terms_list = [[k] for k in range(1, 15)]\n",
    "num_terms = 400\n",
    "analyze_initial_terms(initial_terms_list, num_terms, min_repeat_len=20)\n",
    "print()\n",
    "\n",
    "# 一般情形\n",
    "num_random_examples = 10\n",
    "initial_terms_list = [generate_random_initial_terms(random.randint(2,5)) for _ in range(num_random_examples)]\n",
    "num_terms = 400\n",
    "analyze_initial_terms(initial_terms_list, num_terms, min_repeat_len=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基本规律：\n",
    "- “循环节”的长度和初始项的值有关，且为初始值的最大值\n",
    "- 循环节的内容均为 `1, 2, 3, ..., n` 的置换\n",
    "- 循环的开始位置以平方速度增长（指导下标构造）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3 -- 奇偶序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial Terms: [1]\n",
      "Full Sequence: [1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1]\n",
      "Odd Sequence: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
      "Even Sequence: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n",
      "\n",
      "Initial Terms: [2]\n",
      "Full Sequence: [2, 1, 1, 2, 2, 3, 1, 3, 2, 4, 1, 4, 2, 5, 1, 5, 2, 6, 1, 6, 2, 7, 1, 7, 2, 8, 1, 8, 2, 9, 1, 9, 2, 10, 1, 10, 2, 11, 1, 11]\n",
      "Odd Sequence: [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]\n",
      "Even Sequence: [1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11]\n",
      "\n",
      "Initial Terms: [3]\n",
      "Full Sequence: [3, 1, 1, 2, 1, 3, 2, 2, 3, 3, 4, 1, 4, 2, 4, 3, 5, 1, 5, 2, 5, 3, 6, 1, 6, 2, 6, 3, 7, 1, 7, 2, 7, 3, 8, 1, 8, 2, 8, 3]\n",
      "Odd Sequence: [3, 1, 1, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8]\n",
      "Even Sequence: [1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
      "\n",
      "Initial Terms: [4]\n",
      "Full Sequence: [4, 1, 1, 2, 1, 3, 1, 4, 2, 2, 3, 2, 4, 3, 3, 4, 4, 5, 1, 5, 2, 5, 3, 5, 4, 6, 1, 6, 2, 6, 3, 6, 4, 7, 1, 7, 2, 7, 3, 7]\n",
      "Odd Sequence: [4, 1, 1, 1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3]\n",
      "Even Sequence: [1, 2, 3, 4, 2, 2, 3, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7]\n",
      "\n",
      "Initial Terms: [5]\n",
      "Full Sequence: [5, 1, 1, 2, 1, 3, 1, 4, 1, 5, 2, 2, 3, 2, 4, 2, 5, 3, 3, 4, 3, 5, 4, 4, 5, 5, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 7, 1, 7, 2]\n",
      "Odd Sequence: [5, 1, 1, 1, 1, 2, 3, 4, 5, 3, 3, 4, 5, 6, 6, 6, 6, 6, 7, 7]\n",
      "Even Sequence: [1, 2, 3, 4, 5, 2, 2, 2, 3, 4, 5, 4, 5, 1, 2, 3, 4, 5, 1, 2]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from basic import analyze_sequences, generate_sequence, split_sequence\n",
    "\n",
    "def analyze_and_print(initial_terms_list, num_terms, min_repeat_len=None):\n",
    "    for initial_terms in initial_terms_list:\n",
    "        # Generate the full sequence based on initial terms\n",
    "        full_sequence = generate_sequence(initial_terms, num_terms)\n",
    "        # Split the sequence into odd and even indexed sequences\n",
    "        odd_sequence, even_sequence = split_sequence(full_sequence)\n",
    "        \n",
    "        # Print the initial terms and corresponding generated sequences\n",
    "        print(f\"Initial Terms: {initial_terms}\")\n",
    "        print(f\"Full Sequence: {full_sequence}\")\n",
    "        print(f\"Odd Sequence: {odd_sequence}\")\n",
    "        print(f\"Even Sequence: {even_sequence}\")\n",
    "        print()\n",
    "\n",
    "# 特殊情形\n",
    "initial_terms_list = [[1], [2], [3], [4], [5]]\n",
    "num_terms = 40\n",
    "analyze_and_print(initial_terms_list, num_terms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial Terms: [2, 5, 4, 1]\n",
      "Full Sequence: [2, 5, 4, 1, 1, 2, 2, 3, 1, 3, 2, 4, 2, 5, 2, 6, 1, 4, 3, 3, 4, 4, 5, 3, 5, 4, 6, 2, 7, 1, 5, 5, 6, 3, 6, 4, 7, 2, 8, 1]\n",
      "Odd Sequence: [2, 4, 1, 2, 1, 2, 2, 2, 1, 3, 4, 5, 5, 6, 7, 5, 6, 6, 7, 8]\n",
      "Even Sequence: [5, 1, 2, 3, 3, 4, 5, 6, 4, 3, 4, 3, 4, 2, 1, 5, 3, 4, 2, 1]\n",
      "\n",
      "Initial Terms: [3, 2, 4, 4, 5]\n",
      "Full Sequence: [3, 2, 4, 4, 5, 1, 1, 2, 2, 3, 2, 4, 3, 3, 4, 4, 5, 2, 5, 3, 5, 4, 6, 1, 3, 6, 2, 6, 3, 7, 1, 4, 7, 2, 7, 3, 8, 1, 5, 5]\n",
      "Odd Sequence: [3, 4, 5, 1, 2, 2, 3, 4, 5, 5, 5, 6, 3, 2, 3, 1, 7, 7, 8, 5]\n",
      "Even Sequence: [2, 4, 1, 2, 3, 4, 3, 4, 2, 3, 4, 1, 6, 6, 7, 4, 2, 3, 1, 5]\n",
      "\n",
      "Initial Terms: [1, 3, 1, 3]\n",
      "Full Sequence: [1, 3, 1, 3, 2, 1, 3, 3, 4, 1, 4, 2, 2, 3, 5, 1, 5, 2, 4, 3, 6, 1, 6, 2, 5, 3, 7, 1, 7, 2, 6, 3, 8, 1, 8, 2, 7, 3, 9, 1]\n",
      "Odd Sequence: [1, 1, 2, 3, 4, 4, 2, 5, 5, 4, 6, 6, 5, 7, 7, 6, 8, 8, 7, 9]\n",
      "Even Sequence: [3, 3, 1, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 一般情形\n",
    "num_random_examples = 3\n",
    "initial_terms_list = [generate_random_initial_terms(random.randint(2,5), 5) for _ in range(num_random_examples)]\n",
    "num_terms = 40\n",
    "analyze_and_print(initial_terms_list, num_terms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "思路更新：\n",
    "1. 容易归纳 `N=1` 时 `[k]` 的奇偶序列公式：\n",
    "   - `[1]` 的奇偶序列的公式分别为 `n`, `1`\n",
    "   - 循环开始后，循环部分以 1, 2, ..., n, 1, 2,... 的轮换进行，非循环部分对应以 a, a, ..., a, a+1, a+1, ..., a+1,... 的规律进行\n",
    "   - 循环前的部分通过上一实验，预判长度，然后进一步观察规律和归纳\n",
    "2. 根据题目， N>1 的集合不太可能用归纳降阶，所以 N=1 的公式作用不大，因此不做深入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4 -- 循环节观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial terms: [2, 3, 5, 5, 7]\n",
      "Cycle Start Position: 30\n",
      "Cycle content: [6, 7, 4, 5, 2, 3, 1]\n",
      "\n",
      "6   7   8   8   10  10  11  8   8   9   9   11  11  12  9   9   10  10  12  12  13  10  10  11  11  \n",
      "  6   7   4   5   2   3   1   6   7   4   5   2   3   1   6   7   4   5   2   3   1   6   7   4   5   \n",
      "...\n",
      "\n",
      "Initial terms: [3, 6, 6]\n",
      "Cycle Start Position: 22\n",
      "Cycle content: [6, 2, 3, 1, 4, 5]\n",
      "\n",
      "6   2   3   1   4   5   6   2   3   1   4   5   6   2   3   1   4   5   6   2   3   1   4   5   6   \n",
      "  8   8   9   7   7   7   9   9   10  8   8   8   10  10  11  9   9   9   11  11  12  10  10  10  12  \n",
      "...\n",
      "\n",
      "Initial terms: [5, 4, 5, 1]\n",
      "Cycle Start Position: 14\n",
      "Cycle content: [5, 1, 3, 4, 2]\n",
      "\n",
      "2   7   6   6   7   6   8   7   7   8   7   9   8   8   9   8   10  9   9   10  9   11  10  10  11  \n",
      "  5   1   3   4   2   5   1   3   4   2   5   1   3   4   2   5   1   3   4   2   5   1   3   4   2   \n",
      "...\n",
      "\n",
      "Initial terms: [4, 1, 6, 3]\n",
      "Cycle Start Position: 21\n",
      "Cycle content: [5, 6, 4, 2, 3, 1]\n",
      "\n",
      "5   6   4   2   3   1   5   6   4   2   3   1   5   6   4   2   3   1   5   6   4   2   3   1   5   \n",
      "  6   7   8   8   9   7   7   8   9   9   10  8   8   9   10  10  11  9   9   10  11  11  12  10  10  \n",
      "...\n",
      "\n",
      "Initial terms: [5, 8, 1, 7]\n",
      "Cycle Start Position: 31\n",
      "Cycle content: [6, 7, 2, 3, 4, 5, 1, 8]\n",
      "\n",
      "6   7   2   3   4   5   1   8   6   7   2   3   4   5   1   8   6   7   2   3   4   5   1   8   6   \n",
      "  8   9   9   9   9   10  8   9   9   10  10  10  10  11  9   10  10  11  11  11  11  12  10  11  11  \n",
      "...\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from basic import generate_sequence, split_sequence\n",
    "\n",
    "def format_sequence(sequence, max_length_per_item):\n",
    "    \"\"\"\n",
    "    Format each element in the sequence to a fixed width. \n",
    "    If the element's string representation is longer than the max_length_per_item, truncate it.\n",
    "    \"\"\"\n",
    "    formatted_sequence = []\n",
    "    for item in sequence:\n",
    "        str_item = str(item)\n",
    "        if len(str_item) > max_length_per_item:\n",
    "            str_item = str_item[:max_length_per_item-1] + '…'  # Truncate and add an ellipsis\n",
    "        formatted_sequence.append(f\"{str_item:<{max_length_per_item}}\")\n",
    "    return formatted_sequence\n",
    "\n",
    "def print_sequence_special_format(full_sequence, max_length_per_item=4, max_print_width=40, max_display_lines=10):\n",
    "    # Step 2: Split the sequence into odd and even indexed sequences\n",
    "    odd_sequence, even_sequence = split_sequence(full_sequence)\n",
    "    \n",
    "    # Step 3: Format the sequences\n",
    "    formatted_odd_sequence = format_sequence(odd_sequence, max_length_per_item)\n",
    "    formatted_even_sequence = format_sequence(even_sequence, max_length_per_item)\n",
    "    \n",
    "    # Step 4: Print the formatted sequences within the width and line limit\n",
    "    lines_printed = 0\n",
    "    i = 0\n",
    "    \n",
    "    while i < len(formatted_odd_sequence) or i < len(formatted_even_sequence):\n",
    "        if lines_printed >= max_display_lines * 2:\n",
    "            print(\"...\")  # Add an ellipsis if we exceed the max number of display lines\n",
    "            break\n",
    "        \n",
    "        # Prepare lines\n",
    "        odd_line = \"\".join(formatted_odd_sequence[i:i + max_print_width // max_length_per_item])\n",
    "        even_line = \"\".join(formatted_even_sequence[i:i + max_print_width // max_length_per_item])\n",
    "        \n",
    "        # Print lines\n",
    "        print(odd_line[:max_print_width])\n",
    "        lines_printed += 1\n",
    "        \n",
    "        if lines_printed < max_display_lines * 2:  # Ensure even line is printed within the display line limit\n",
    "            print(\"  \" + even_line[:max_print_width])\n",
    "            lines_printed += 1\n",
    "        \n",
    "        i += max_print_width // max_length_per_item\n",
    "\n",
    "\n",
    "def extract_and_print_cycles(initial_terms, num_terms, min_repeat_len=None, nchuncks=-1):\n",
    "    # Analyze sequence to get cycle information\n",
    "    result = analyze_sequences(initial_terms, num_terms, min_repeat_len)\n",
    "    sequence_type, cycle_start, cycle_content = result\n",
    "    \n",
    "    # Generate full sequence\n",
    "    full_sequence = generate_sequence(initial_terms, num_terms)\n",
    "    \n",
    "    # Extract the cyclic part of the sequence starting from the cycle_start\n",
    "    cyclic_sequence = full_sequence[cycle_start * 2:]\n",
    "    \n",
    "    # Split cyclic sequence into odd and even indexed sequences\n",
    "    odd_sequence, even_sequence = split_sequence(cyclic_sequence)\n",
    "    \n",
    "    # Determine the period length\n",
    "    cycle_length = len(cycle_content)\n",
    "    \n",
    "    # Print results in interleaved fashion\n",
    "    print(f\"Initial terms: {initial_terms}\")\n",
    "    print(f\"Cycle Start Position: {cycle_start}\")\n",
    "    print(f\"Cycle content: {cycle_content}\\n\")\n",
    "    print_sequence_special_format(cyclic_sequence, max_print_width=100, max_display_lines=1)\n",
    "    print()\n",
    "\n",
    "# 示例初始条件\n",
    "num_random_examples = 5\n",
    "initial_terms_list = [generate_random_initial_terms(random.randint(3,6), 8) for _ in range(num_random_examples)]\n",
    "num_terms = 500\n",
    "\n",
    "# 调用函数进行分析和打印\n",
    "for initial_terms in initial_terms_list:\n",
    "    _ = extract_and_print_cycles(initial_terms, num_terms, nchuncks=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设循环序列为 `x_n` 非循环序列为 `y_n` 分析规律：\n",
    "1. `y_n` 计数了 `x_n` 的数目，由 `x_n` 周期性 => `y_n` 每个周期比之前增 1，比如\n",
    "   - `6   7   8   10  10`\n",
    "   - `10  11  8   8   9`\n",
    "   - `11  11  11  12  9 `\n",
    "2. `x_n` 计数了 `y_n` 的数目，由 `x_n` 上界 => `y_n` 数目上界"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5 -- 循环节分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial terms: [8, 6, 2, 1, 5]\n",
      "Cycle Start Position: 39\n",
      "Cycle content: [7, 8, 6, 3, 4, 5, 2, 1]\n",
      "\n",
      "[7, 8, 6, 3, 4, 5, 2, 1]\n",
      "[0, 1, 2, 2, 2, 3, 4, 1]\n",
      "\n",
      "[7, 8, 6, 3, 4, 5, 2, 1]\n",
      "[0, 1, 2, 2, 2, 3, 4, 1]\n",
      "\n",
      "[7, 8, 6, 3, 4, 5, 2, 1]\n",
      "[0, 1, 2, 2, 2, 3, 4, 1]\n",
      "\n",
      "Initial terms: [2, 5]\n",
      "Cycle Start Position: 12\n",
      "Cycle content: [4, 5, 2, 1, 3]\n",
      "\n",
      "[0, 1, 2, 3, 2]\n",
      "[4, 5, 2, 1, 3]\n",
      "\n",
      "[0, 0, 1, 2, 1]\n",
      "[4, 5, 2, 1, 3]\n",
      "\n",
      "[0, 0, 1, 2, 1]\n",
      "[4, 5, 2, 1, 3]\n",
      "\n",
      "Initial terms: [7, 4]\n",
      "Cycle Start Position: 24\n",
      "Cycle content: [6, 7, 2, 3, 4, 1, 5]\n",
      "\n",
      "[0, 1, 2, 2, 2, 3, 2]\n",
      "[6, 7, 2, 3, 4, 1, 5]\n",
      "\n",
      "[0, 0, 1, 1, 1, 2, 1]\n",
      "[6, 7, 2, 3, 4, 1, 5]\n",
      "\n",
      "[0, 0, 1, 1, 1, 2, 1]\n",
      "[6, 7, 2, 3, 4, 1, 5]\n",
      "\n",
      "Initial terms: [5, 4, 3, 6, 2, 3]\n",
      "Cycle Start Position: 25\n",
      "Cycle content: [5, 4, 3, 2, 1, 6]\n",
      "\n",
      "[5, 4, 3, 2, 1, 6]\n",
      "[1, 2, 4, 5, 0, 1]\n",
      "\n",
      "[5, 4, 3, 2, 1, 6]\n",
      "[1, 2, 4, 5, 0, 1]\n",
      "\n",
      "[5, 4, 3, 2, 1, 6]\n",
      "[1, 2, 4, 5, 0, 1]\n",
      "\n",
      "Initial terms: [8, 3, 3, 4, 4]\n",
      "Cycle Start Position: 37\n",
      "Cycle content: [7, 8, 4, 2, 3, 1, 5, 6]\n",
      "\n",
      "[7, 8, 4, 2, 3, 1, 5, 6]\n",
      "[0, 1, 3, 2, 4, 1, 1, 1]\n",
      "\n",
      "[7, 8, 4, 2, 3, 1, 5, 6]\n",
      "[0, 1, 3, 2, 4, 1, 1, 1]\n",
      "\n",
      "[7, 8, 4, 2, 3, 1, 5, 6]\n",
      "[0, 1, 3, 2, 4, 1, 1, 1]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from basic import analyze_sequences, generate_sequence, split_sequence\n",
    "\n",
    "def extract_and_print_cycles(initial_terms, num_terms, min_repeat_len=None, nchuncks=-1):\n",
    "    # Analyze sequence to get cycle information\n",
    "    result = analyze_sequences(initial_terms, num_terms, min_repeat_len)\n",
    "    sequence_type, cycle_start, cycle_content = result\n",
    "    \n",
    "    # Generate full sequence\n",
    "    full_sequence = generate_sequence(initial_terms, num_terms)\n",
    "    \n",
    "    # Extract the cyclic part of the sequence starting from the cycle_start\n",
    "    cyclic_sequence = full_sequence[cycle_start * 2:]\n",
    "    \n",
    "    # Split cyclic sequence into odd and even indexed sequences\n",
    "    odd_sequence, even_sequence = split_sequence(cyclic_sequence)\n",
    "    \n",
    "    # Determine the period length\n",
    "    cycle_length = len(cycle_content) \n",
    "    \n",
    "    # Cut sequences into chunks of cycle length\n",
    "    odd_chunks = [odd_sequence[i:i+cycle_length] for i in range(0, len(odd_sequence), cycle_length)]\n",
    "    even_chunks = [even_sequence[i:i+cycle_length] for i in range(0, len(even_sequence), cycle_length)]\n",
    "    \n",
    "    # Print results in interleaved fashion\n",
    "    print(f\"Initial terms: {initial_terms}\")\n",
    "    print(f\"Cycle Start Position: {cycle_start}\")\n",
    "    print(f\"Cycle content: {cycle_content}\\n\")\n",
    "    for i in range(max(len(odd_chunks), len(even_chunks))):\n",
    "        if nchuncks != -1 and i >= nchuncks: break\n",
    "        if i < len(odd_chunks):\n",
    "            chunks = [num - min(odd_chunks[i]) for num in odd_chunks[i]] if sequence_type != \"odd\" else odd_chunks[i]\n",
    "            print(chunks)\n",
    "        if i < len(even_chunks):\n",
    "            chunks = [num - min(even_chunks[i]) for num in even_chunks[i]] if sequence_type != \"even\" else even_chunks[i]\n",
    "            print(chunks)\n",
    "        print(\"\")  # Add a blank line between each chunk for readability\n",
    "    return odd_sequence, even_sequence\n",
    "\n",
    "# 示例初始条件\n",
    "num_random_examples = 5\n",
    "initial_terms_list = [generate_random_initial_terms(random.randint(2,6), 8) for _ in range(num_random_examples)]\n",
    "num_terms = 500\n",
    "\n",
    "# 调用函数进行分析和打印\n",
    "for initial_terms in initial_terms_list:\n",
    "    _ = extract_and_print_cycles(initial_terms, num_terms, nchuncks=3, min_repeat_len=20)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
